#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    buildMap();
    on_calendarWidget_selectionChanged();
}

Widget::~Widget()
{

    delete ui;
}


void Widget::on_calendarWidget_selectionChanged()
{
    QString value = ui->textEditEvent->toPlainText();
    if(value.size()>0)
        map.insert(key, value);
    else 
        map.remove(key);
    date = ui->calendarWidget->selectedDate();
    key = date.toString("dd.MM.yyyy");
    if(map.contains(key))
        ui->textEditEvent->setText(map.value(key));
    else
        ui->textEditEvent->setText("");

    QDate firstInInterval = date.addDays(-4);
    QDate lastInInterval = date.addDays(6);
    int row = 0;
    ui->tableWidgetMonth->clear();

    for(int i = firstInInterval.toJulianDay(); i < lastInInterval.toJulianDay(); i++)
    {
        QString currentDate = QDate::fromJulianDay(i).toString("dd.MM.yyyy");
        if(map.contains(currentDate))
        {

            QTableWidgetItem *newItem1 = new QTableWidgetItem(currentDate);
            QTableWidgetItem *newItem2 = new QTableWidgetItem(map.value(currentDate));
            ui->tableWidgetMonth->setItem(row, 0, newItem1);
            ui->tableWidgetMonth->setItem(row, 1, newItem2);
            if(key == currentDate)
                ui->tableWidgetMonth->selectRow(row);
            row++;
        }
    }

}

void Widget::on_tableWidgetMonth_cellClicked (int row, int column )
{
    QTableWidgetItem *item = ui->tableWidgetMonth->item(row,0);
    if(!item)
        return;
    QStringList date = item->text().split(".");
    QDate newDate = QDate(date.at(2).toInt(), date.at(1).toInt(), date.at(0).toInt());
    ui->calendarWidget->setSelectedDate(newDate);
    on_calendarWidget_selectionChanged();
}

bool Widget::buildMap()
{
    QFile file("calendar.xml");
    file.open(QFile::ReadOnly | QFile::Text);
    reader.setDevice(&file);
    while(!reader.atEnd())
    {
        reader.readNext();
        if(reader.name()=="calendar")
        {
            while(!reader.atEnd())
            {
                reader.readNext();
                if(reader.name() == "item")
                {
                    QString date = reader.attributes().value("date").toString();
                    QString event;
                    while (!reader.atEnd())
                    {
                         reader.readNext();
                         if (reader.isEndElement())
                             break;
                         if (reader.isStartElement())
                         {
                             event = reader.readElementText();
                         }
                     }
                    map.insert(date, event );
                }
            }
        }
    }
    return !reader.error();
}

void Widget::save()
{
    date = ui->calendarWidget->selectedDate();
    key = date.toString("dd.MM.yyyy");
    QString value = ui->textEditEvent->toPlainText();
    if(value.size()>0)
        map.insert(key, value);

    QFile file("calendar.xml");
    file.open(QFile::WriteOnly | QFile::Text);
    writer.setDevice(&file);
    writer.setAutoFormatting(true);
    writer.writeStartDocument();
    writer.writeStartElement("calendar");
    QMapIterator<QString, QString> i(map);
    while (i.hasNext()) {
        i.next();
        writer.writeStartElement("item");
        writer.writeAttribute("date", i.key() );
        writer.writeTextElement("event", i.value());
        writer.writeEndElement();
    }
    writer.writeEndElement();
    writer.writeEndDocument();
}

void Widget::closeEvent(QCloseEvent *e)
{
    save();
    e->accept();
}
